package com.cy1640.damai;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;

import com.cy1640.damai.bean.BannerBean;
import com.cy1640.damai.ui.recommand.ClassfyView;
import com.cy1640.damai.ui.recommand.ClassifyBean;
import com.cy1640.damai.ui.recommand.DividerView;
import com.cy1640.damai.ui.recommand.HeadLineBean;
import com.cy1640.damai.ui.recommand.TypeView1;
import com.cy1640.damai.ui.recommand.TypeViewBean;
import com.cy1640.damai.util.NetConfig;
import com.cy1640.damai.util.NetResponse;
import com.cy1640.damai.util.NetUtils;
import com.cy1640.damai.util.ToastUtils;
import com.cy1640.damai.view.BannerView;
import com.cy1640.damai.view.BannerViewPager;
import com.google.gson.Gson;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class RecommendFragment extends Fragment {

    private ArrayList<BannerBean> banners=new ArrayList<>();
    private ArrayList<String> bannerStr=new ArrayList<>();

    private ArrayList<ClassifyBean> classifys=new ArrayList<>();
    private ArrayList<HeadLineBean> headLines=new ArrayList<>();
//    private ViewGroup rootView;
    private Gson gson=new Gson();

    private ClassfyView classfyView;
    private LinearLayout recommandContainer;

    public RecommendFragment() {
    }



    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return  inflater.inflate(R.layout.fragment_recommend, container, false);
    }

    private NetUtils netUtils;

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
         netUtils = NetUtils.getInstance();
        classfyView= (ClassfyView) view.findViewById(R.id.classifyView);
        recommandContainer= (LinearLayout) view.findViewById(R.id.recommandContainer);
        initBannerView(view);
        loadBanner();
    }

    BannerView bannerView;
    private void initBannerView(View v) {
         bannerView = (BannerView) v.findViewById(R.id.bannerView);
        bannerView.setOnBannerViewClick(new BannerViewPager.OnBannerViewClick() {
            @Override
            public void onBannerViewClick(int position) {
                ToastUtils.show(banners.get(position).getName());
            }
        });
    }

    public void loadData(){
       HashMap<String,String> maps=new HashMap<>();
        maps.put("cityID","852");
        netUtils.get("Proj/Panev3.aspx", maps, new NetResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONObject object=new JSONObject(response);

                    fillClassifyView(object);

                    JSONArray array=object.getJSONArray("list");
                    for (int i = 0; i <array.length() ; i++) {
                        TypeViewBean typeViewBean=gson.fromJson(array.getString(i),TypeViewBean.class);
                        switch (typeViewBean.getType()) {
                            case 1:
                                TypeView1 v1=new TypeView1(getContext());
                                v1.setData(typeViewBean);
                                recommandContainer.addView(v1);
                                recommandContainer.addView(new DividerView(getContext()));
                                break;
                            case 2:

                                break;
                            case 3:

                                break;
                        }
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String erroe) {

            }
        });
    }

    public void fillClassifyView(JSONObject object) throws JSONException {
            JSONArray btnCtx=object.getJSONArray("btnCtx");
            classifys.clear();
            for (int i = 0; i <btnCtx.length() ; i++) {
                classifys.add(gson.fromJson(btnCtx.getString(i),ClassifyBean.class));
            }
            classfyView.setClassifys(classifys);
            headLines.clear();
            JSONArray headLine=object.getJSONArray("headline");
            for (int i = 0; i <headLine.length() ; i++) {
                headLines.add(gson.fromJson(headLine.getString(i),HeadLineBean.class));
            }
            classfyView.setHeadLines(headLines);
    }
    private void loadBanner(){
        HashMap<String,String> maps=new HashMap<>();
        maps.put("cityId","852");

        netUtils.get(NetConfig.BASE_URL2,
                "index/banner/13/list.json",
                maps, new NetResponse() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray array=new JSONArray(response);
                    Gson gson=new Gson();
                    banners.clear();
                    bannerStr.clear();
                    for (int i = 0 ,len=10; i < len ; i++) {
                        banners.add(gson.fromJson(array.getString(i),BannerBean.class));
                        bannerStr.add(array.getJSONObject(i).getString("Pic"));
                    }
                    bannerView.setData(bannerStr);

                    //再加载页面数据
                    loadData();
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String erroe) {

            }
        });
    }
}
